/*
    Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)

    This file is part of the Semantic Discovery Toolkit.

    The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace SemanticDiscovery.Util {
  /**
   * Utility class to format dates and times.
   * <p>
   * @author Spence Koehler
   */
  public class DateUtil {
    public static DateTime UnixEpoch = 
      new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    //public static final Calendar CURRENT_CALENDAR = new GregorianCalendar();
    //public static final int CURRENT_YEAR = CURRENT_CALENDAR.get(Calendar.YEAR);

    /// Build a date string of the form yyyy-mm-dd-hh:mm:ss
    public static string BuildDateString(long millis) {
      DateTime dateTime = UnixEpoch.AddMilliseconds(millis);

      return BuildDateString(dateTime.Year,
                             dateTime.Month,
                             dateTime.Day,
                             dateTime.Hour,
                             dateTime.Minute,
                             dateTime.Second);
    }

    /// Build a string of the form yyyy-mm-dd-hh:mm:ss
    /// @param year   4-digit year
    /// @param month  month (1-based, January=1)
    /// @param day    day of month (1-based)
    /// @param hour   24-hour clock-based hour (0-23)
    /// @param minute minute in hour (0-based)
    /// @param second second in minute (0-based)
    public static string BuildDateString(int? year, 
                                         int? month, 
                                         int? day, 
                                         int? hour, 
                                         int? minute, 
                                         int? second) {
      StringBuilder result = new StringBuilder();

      result.Append(AsDigits(year.Value, 4));
      if (month.HasValue) {
        result.AppendFormat("-{0}", AsDigits(month.Value, 2));
        if (day.HasValue) {
          result.AppendFormat("-{0}", AsDigits(day.Value, 2));
          if (hour.HasValue) {
            result.AppendFormat("-{0}", AsDigits(hour.Value, 2));
            if (minute.HasValue) {
              result.AppendFormat(":{0}", AsDigits(minute.Value, 2));
              if (second.HasValue) {
                result.AppendFormat(":{0}", AsDigits(second.Value, 2));
              }
            }
          }
        }
      }

      return result.ToString();
    }

    /// Create a string of the form yyyy-mm-dd from the time indicated by the millis.
    public static string BuildYMDString(long millis) {
      DateTime dateTime = UnixEpoch.AddMilliseconds(millis);

      return BuildDateString(dateTime.Year,
                             dateTime.Month,
                             dateTime.Day,
                             null, null, null);
    }

    public static string AsDigits(int number, int minNumDigits) {
      StringBuilder result = new StringBuilder();

      result.Append(number.ToString());
      int len = result.Length;
      for (int i = minNumDigits - len; i > 0; --i) 
        result.Insert(0, '0');

      return result.ToString();
    }
  }
}
